//
//  MsgUserVoiceCell.swift
//  swiftim
//
//  Created by yangjian on 2023/6/14.
//

import Foundation


class MsgUserVoiceCell : MsgUserBaseCell {
    
//    @property (nonatomic,strong)UIImageView *waveImageView;
//    @property (nonatomic,strong)UILabel *durationLabel;
//    @property (nonatomic,strong)UILabel *convertTextLabel;
//
//
//    @property (nonatomic,strong)UIView *voiceContentView;
//    @property (nonatomic,strong)UIView *convertTextContentView;
//
//
//    @property (nonatomic,strong)UIImageView *bubbleView;
//
//    @property (nonatomic)EMVoiceConvertTextState voiceConvertTextState;

//    let voiceContentView = UIView()
    let convertTextContentView = UIView()
    let waveIV = UIImageView()
    let durationLabel = UILabel()
    let textView = IMMsgTextView()
    let convertState = MsgVoiceConvertTextStatus.none
    
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.configMsgSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configMsgSubViews() {
        configBubbleView(superView: msgBackgroundView, aboveView: msgContentView)
        msgContentView.addSubview(waveIV)
        msgContentView.addSubview(durationLabel)
        
        msgBackgroundView.addSubview(convertTextContentView)
        convertTextContentView.addSubview(textView)

        stateLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self.msgContentView.snp.bottom)
            make.right.equalTo(self.msgContentView.snp.left).offset(-20)
        }
        
        
        //这个约束其实不需要加,但是不加会在console输出一堆约束有问题,比较烦,所以临时加上,其实也没用到
        convertTextContentView.snp.makeConstraints { make in
            make.top.equalTo(0);
            make.left.equalTo(0);
        }
        
        textView.snp.makeConstraints { make in
            make.top.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextEdgeSpacing)
            make.bottom.equalTo(-MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextEdgeSpacing)
            make.left.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextEdgeSpacing)
            make.right.equalTo(-MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextEdgeSpacing)
        }
        
        msgContentView.isUserInteractionEnabled = false
        
        convertTextContentView.backgroundColor = UIColor(red: 0.88, green: 0.88, blue: 0.88, alpha: 1)
        convertTextContentView.layer.cornerRadius = 6
        convertTextContentView.layer.masksToBounds = true
        convertTextContentView.isHidden = true
        
        durationLabel.font = UIFont.systemFont(ofSize: 18)
        durationLabel.textColor = UIColor.black

        waveIV.animationDuration = 1.0;
        waveIV.animationRepeatCount = 0;

        
        textView.font = MsgTableViewConfig.share.voiceConvertTextFont
        textView.setContentCompressionResistancePriority(.required, for: .horizontal)
        textView.setContentCompressionResistancePriority(.required, for: .vertical)
        textView.setContentHuggingPriority(.required, for: .horizontal)
        textView.setContentHuggingPriority(.required, for: .vertical)
        textView.rx_msgTextSelectionEventSubject.map {[unowned self] (event,text) in
            return (self.model,self,event,text)
        }.bind(to: self.rx_msgEventSubject).disposed(by: bag)
    }
    

    override func resetSubViewsLayout(showName: Bool, showHead: Bool) {
        super.resetSubViewsLayout(showName: showName, showHead: showHead)
        resetBubbleleView(aboveView: msgContentView)

        let waveAndDurationSpacing = 4.0
        
        if model.direction == .send {
            durationLabel.snp.remakeConstraints { make in
                make.centerY.equalTo(self.msgContentView)
                make.left.equalTo(0)
                make.right.equalTo(-(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight + waveAndDurationSpacing))
            }
            waveIV.snp.remakeConstraints { make in
                make.top.equalTo(0)
                make.bottom.equalTo(0)
                make.right.equalTo(0)
                make.width.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight)
                make.height.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight)
            }
            
            if model.voiceConvertText_status == .none {
                msgContentView.snp.remakeConstraints { make in
                    make.top.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.top)
                    make.bottom.equalTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.left.greaterThanOrEqualTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.toSide)
                    make.right.equalTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.fromSide)
                }
                convertTextContentView.isHidden = true
            }else{
                msgContentView.snp.remakeConstraints { make in
                    make.top.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.top)
//                    make.bottom.lessThanOrEqualTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.left.greaterThanOrEqualTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.toSide)
                    make.right.equalTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.fromSide)
                }
                convertTextContentView.isHidden = false
                convertTextContentView.snp.remakeConstraints { make in
                    make.top.equalTo(self.msgContentView.snp.bottom).offset(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.bottom.equalTo(-MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextToBottom);
                    make.left.greaterThanOrEqualTo(0);
                    make.right.equalTo(0);
                }
            }
            durationLabel.textAlignment = .right
            waveIV.image = send_defaultImage
            waveIV.animationImages = send_images

            
            
        }else if model.direction == .receive {
            durationLabel.snp.remakeConstraints { make in
                make.centerY.equalTo(msgContentView)
                make.left.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight + waveAndDurationSpacing)
                make.right.equalTo(0)
            }
            waveIV.snp.remakeConstraints { make in
                make.top.equalTo(0)
                make.bottom.equalTo(0)

                make.left.equalTo(0)
                make.width.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight)
                make.height.equalTo(MsgCellOtherLayoutAdapterConfig.share.voiceContentViewHeight)
            }
            
            if model.voiceConvertText_status == .none {
                msgContentView.snp.remakeConstraints { make in
                    make.top.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.top)
                    make.bottom.equalTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.left.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.fromSide)
                    make.right.lessThanOrEqualTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.toSide)
                }
                convertTextContentView.isHidden = true
            }else{
                msgContentView.snp.remakeConstraints { make in
                    make.top.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.top)
//                    make.bottom.lessThanOrEqualTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.left.equalTo(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.fromSide)
                    make.right.lessThanOrEqualTo(-MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.toSide)
                }

                convertTextContentView.isHidden = false
                convertTextContentView.snp.remakeConstraints { make in
                    make.top.equalTo(self.msgContentView.snp.bottom).offset(MsgCellLayoutAdapterConfig.share.contentLayoutAdapter.bottom);
                    make.bottom.equalTo(-MsgCellOtherLayoutAdapterConfig.share.voiceConvertTextToBottom);
                    make.left.equalTo(0);
                    make.right.lessThanOrEqualTo(0);
                }
            }
            
            durationLabel.textAlignment = .left
            waveIV.image = receive_defaultImage
            waveIV.animationImages = receive_images

        }
        
    }
    
    override func bindFromModel(_ model: MsgCellBaseModel) {
//        model.text = "testtes ttest tes ttese stte sttesttestt esttestte sttese sttes ttesttes ttesttes ttesttese sttestt estte sttestt estte sttese sttes ttest"
//        model.voiceConvertText_status = .success
        super.bindFromModel(model)
        self.durationLabel.text = "\(model.voiceDuration)\""
        self.textView.text = model.text
        if model.voiceConvertText_status == .none {
        }else if model.voiceConvertText_status == .converting {
            self.textView.textColor = UIColor.gray
        }else if model.voiceConvertText_status == .success {
            self.textView.textColor = UIColor.black
        }else if model.voiceConvertText_status == .failure {
            self.textView.textColor = UIColor.red
        }
        model.rx_voicePlaying.subscribe(onNext: {playing in
            if playing {
                self.waveIV.startAnimating()
            }else{
                self.waveIV.stopAnimating()
            }
        }).disposed(by: reuseBag)
    }
    
    let send_images = [UIImage(named: "m_voice_send_0") ?? UIImage(),UIImage(named: "m_voice_send_1") ?? UIImage(),UIImage(named: "m_voice_send_2") ?? UIImage()]
    let receive_images = [UIImage(named: "m_voice_receive_0") ?? UIImage(),UIImage(named: "m_voice_receive_1") ?? UIImage(),UIImage(named: "m_voice_receive_2") ?? UIImage()]
    let send_defaultImage = UIImage(named: "m_voice_send_2") ?? UIImage()
    let receive_defaultImage = UIImage(named: "m_voice_receive_2") ?? UIImage()
}


extension MsgUserVoiceCell {
    
    func waveImages(_ direction : EMMessageDirection) -> [UIImage] {
        if direction == .send {
            return send_images
        }else if direction == .receive {
            return receive_images
        }else{return []}
    }
    
    func waveDefaultImage(_ direction : EMMessageDirection) -> UIImage {
        if direction == .send {
            return send_defaultImage
        }else if direction == .receive {
            return receive_defaultImage
        }else{return UIImage()}
    }
}
